#include "drivers.h"

/**
 * @brief  Loop for 3 Machine Cycle In STM32F4 Series and 3 
 *  	   Machine loop in STM32F1 Serires in while.Can be used
 *  	   in driver setup delay,not suiteable for RTOS Task.You
 *  	   will need to define your compiler befor using this
 *  	   function.
 *
 * @param   the cycle you want to loop for diviede by 3
 * @return   none
 *
 */

#if defined(ewarm) || defined(DOXYGEN)
void
SysCtlDelay(unsigned long ulCount)
{
	__asm("    subs    r0, #1\n"
		  "    bne.n   SysCtlDelay\n"
		  "    bx      lr");
}
#endif

#if defined(codered) || defined(gcc) || defined(sourcerygxx)
void __attribute__((naked))
SysCtlDelay(unsigned long ulCount)
{
	__asm("    subs    r0, #1\n"
		  "    bne     SysCtlDelay\n"
		  "    bx      lr");
}
#endif

#if defined(rvmdk) || defined(__ARMCC_VERSION)
__asm void
SysCtlDelay(unsigned long ulCount)
{
	subs    r0, #1;
	bne     SysCtlDelay;
	bx      lr;
}
#endif

/**
 * @brief  Get the System Running clock in HZ
 *
 * @param   none
 * @return   the system running clock in HZ
 * 
 */

uint32_t  Get_SystemClock(void)
{
	RCC_ClocksTypeDef All_Clocks;
	RCC_GetClocksFreq(&All_Clocks);
	return(All_Clocks.SYSCLK_Frequency);
}

/**
 * @brief  Delay certain time in ms
 *
 * @param   the time you want to delay in ms
 * @return   none
 *
 */

void Delay_ms(uint16_t ms)
{
	SysCtlDelay(ms*(Get_SystemClock()/6000));
}



